import $ from '@/common'
import validadte from './validadte'
/*
重置版 开发中...

vaildate 改变情况
正常输入出发验证改变
状态导致非输入状态

*/
export default {
  mixins: [validadte],
  state: {
    enableValidate: true,
    validate: true,
    forceEnable: null,
    fields: {},
    enable: false,
    model: {},
    setting: {
      hasSlave: true
    },
    data: {},
    /*
    vaildate: {
       type: 'email|mobile|...',//验证类型
       max,//最大值
       min,//最小值
       reg_exp//验证正则表达式
    }
    */
  },
  getters: {
    defaultData(state) {
      var data = {}
      if (state.model.fields) {
        for (let field of state.model.fields) {
          data[field.code] = field.def_value
        }
      }
      return data
    },
    dataParam(state, getters) {
      return {
        ...state.data,
        ...getters.parentData
      }
    },
    parentData(state) {
      return {}
    },
    slaveData(state) {
      //临时处理，适应当前版本list store
      var data = {}
      for (let i in state.model.slaves) {
        let code = state.model.slaves[i].code
        if (state[`slave_${code}`]) {
          data[code] = state[`slave_${code}`].listData
        }
      }
      return data
    },
    formData(state, getters) {
      return {
        ...state.data,
        ...getters.slaveData
      }
    }
  },
  actions: {
    updateModel({
      commit,
      state
    }, data) {
      commit('setModel', data)
      commit('resetFields')
    }, // 获取表单数据,以确保完整的获取表单数据
    getData({
      state
    }) {
      return state.data
    },
    getTest({
      dispatch
    }) {
      return dispatch('getData')
    },
    //恢复默认数据，基于def_value生成的默认数据混合输入的额外数据
    restoreDefault({
      state,
      commit,
      getters
    }, ext) {
      var data = {
        ...getters.defaultData,
        ...ext
      }
      var param = {
        ...data,
        ...getters.parentData
      }
      for (let code in data) {
        data[code] = $.composeValue(data[code], param)
      }
      commit('setData', data)
    }
  },
  mutations: {
    setData(state, data) {
      $.set(state, 'data', data)
    },
    // 重置字段状态值
    resetFields(state) {
      var data = {}
      for (let field of state.model.fields) {
        // 设置字段的初始化状态
        data[field.code] = {
          show: field.cardpos !== 0,
          enable: field.editable !== false,
          require: field.required === true,
          validate: true, // 验证状态
          validateType: null, // 验证类型，将决定验证失败时的显示文字
          message: null,
          model: field,
          active: false, // 激活状态 首次赋值或者验证后会打开激活状态
          enableValidate: state.enableValidate // 验证可用状态 不可用时将所有验证结果强制设置ture
        }
      }
      $.set(state, 'fields', data)
    },
    setValue(state, {
      code,
      value
    }) {
      $.set(state.data, code, value)
    },
    importData(state, data) {
      for (let i in data) {
        $.set(state.data, i, data[i])
      }
    },
    // 设置表单模型并进行初始化
    setModel(state, data) {
      $.set(state, 'model', data)
    },
    // 统一改变所有字段的显示状态
    setAllField(state, status) {
      for (let i in state.fields) {
        for (let j in status) {
          $.set(state.fields[i], j, status[j])
        }
      }
    },
    // 设置单一表单状态
    setField(state, status) {
      if (status.code && state.fields[status.code]) {
        for (let i in status) {
          if (status[i] !== state.fields[status.code]) {
            state.fields[status.code] = status[i]
          }
        }
      }
    }
    // 设置表单整体验证状态??
  }

}
